#include "GUI.h"
#include "Global.h"


CGUIButton::CGUIButton(GUI_PARAMS* params, GUI_COLORS* colors, GUI_SPRITES* sprites, UINT _event, void* _dest, LPCSTR _text){
	Init(params, colors, sprites);
	m_Event=_event;
	m_pDest=_dest;
	m_Text=_text;
	m_Type=GUITYPE_BUTTON;
};
CGUIButton::~CGUIButton(){};

bool CGUIButton::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){
	switch(uMsg){
		// Keyboard messages
        case WM_KEYDOWN:
        case WM_SYSKEYDOWN:
        case WM_KEYUP:
        case WM_SYSKEYUP:
			return HandleKeyboard( uMsg, wParam, lParam );
		//Mouse Messages
				case WM_MOUSEMOVE:
        case WM_LBUTTONDOWN:
        case WM_LBUTTONUP:
        case WM_MBUTTONDOWN:
        case WM_MBUTTONUP:
        case WM_RBUTTONDOWN:
        case WM_RBUTTONUP:
        case WM_XBUTTONDOWN:
        case WM_XBUTTONUP:
        case WM_LBUTTONDBLCLK:
        case WM_MBUTTONDBLCLK:
        case WM_RBUTTONDBLCLK:
        case WM_XBUTTONDBLCLK:
        case WM_MOUSEWHEEL:
			return HandleMouse( uMsg, wParam, lParam );
		default:
			return false;
	}
}
bool CGUIButton::HandleKeyboard( UINT uMsg, WPARAM wParam, LPARAM lParam ) {
	if(GetFocus() && wParam==m_Hotkey){
		OnHotkey();
		return true;
	}
	return false;
}
bool CGUIButton::HandleMouse( UINT uMsg, WPARAM wParam, LPARAM lParam ) {

	switch(uMsg){
		case WM_MOUSEMOVE:
			POINT MousePos;
			GetCursorPos(&MousePos);
			if(PtInBB(MousePos) && !m_bMouseOver && GetVisible()){
				OnMouseEnter();
			}if(!PtInBB(MousePos) && m_bMouseOver){
				OnMouseLeave();
			}
			//if(Parent && ((CGUIWindow*)Parent)->GetFocusElement()==this)
				//return true;

			break;
		case WM_LBUTTONDOWN:
			if(m_bMouseOver){
				if(!m_bButtonDown)
					if(GetFocus() || RequestFocus()){
						m_bButtonDown=true;
					}
				return true;
			}
			break;
		case WM_LBUTTONUP:
			if(m_bButtonDown && m_bMouseOver){
				OnHotkey();
				return true;
			}
			m_bButtonDown=false;
		default:
			if(m_bMouseOver)
				return true;
			return false; 
	}
	return false;
}

bool CGUIButton::RequestFocus() {
	if(m_pParent){
		if(CanHaveFocus()){
			return ((CGUIWindow*)m_pParent)->SetFocusElement(this);
		}
	}
	return false;
}
bool CGUIButton::CanHaveFocus(){ return true; }
void CGUIButton::OnFocusIn() { m_bHasFocus = true; }
void CGUIButton::OnFocusOut() { m_bHasFocus = false; }
void CGUIButton::OnMouseEnter() { 
//	SetCursor(g_pCursor);
	m_bMouseOver = true; 
}
void CGUIButton::OnMouseLeave() { m_bMouseOver = false; }
void CGUIButton::OnHotkey() {
	if(OnGUIEvent)
			OnGUIEvent(m_Type, m_Event, this, m_pDest);
}

void CGUIButton::Step(){}
void CGUIButton::Draw(){
	if(GetVisible()){
		UpdateBB();
		if(m_pSprites && m_pSprites->BG)
			DrawSpriteExact(m_pSprites->BG, 0, m_drawpos);
		else
			DrawRectExact(m_drawpos, m_width, m_height, m_pColors->BG, m_border, m_pColors->Border);

		if(m_Text!="")
			DrawTextExact(g_pFontList->Arial20B, m_Text, V(m_drawpos.x+5, m_drawpos.y+2), m_pColors->Text);
		//if(GetFocus())
			//DrawTextExact(g_pFontList->Arial20B, "Has Focus->", dx-128, dy+2, TextColor);

		if(!GetEnabled())
			DrawRectExact(m_drawpos, m_width, m_height, D3DCOLOR_ARGB(128, 128, 128, 128));
	}
}